#!/bin/sh
# Bug reports and comments to Igor.Ermolaev@intel.com
args=( 
   '-env 2' '-envall 0' '-genv 2' '-genvall 0'
   '-n 1' '-np 1' '-machinefile 1' '-f 1' '-hostfile 1' '-hosts 1' '-host 1' '-configfile 1'
   '/mpirun 0' '/mpiexec 0' '/mpirun_rsh 0' '/mpiexec.hydra 0'
   '-r 1'
   '-ppn 1' '-perhost 1' '-grr 1' '-rr 1'
   '-p 0' '--parallel-startup 0'
   '-l 0'
   '-bootstrap 1'
   '-v 0'
   '--totalnum= 0' '--ssh= 0' '--rsh= 0'
   'numactl 2'
)
unset inj
while [ $# -ne 0 ] && [ "$1" != '--' ];do
   inj[${#inj[@]}]="$1"
   shift
done
shift
while [ $# -ne 0 ] && [ "$1" == '--' ];do
   inj[${#inj[@]}]="$1"
   shift
done
unset cmd
while [ $# -ne 0 ];do
   opt=`printf '%s\n' "$1"|sed 's/^\([^=]\+=\).*$/\1/'`
   for i in ${!args[@]};do
      desc=( ${args[$i]} )
      if [ "$opt" == "${desc[0]}" ] || ( [ "${desc[0]:0:1}" == '/' ] && [ "${opt:0:1}" != '-' ] && [ $(basename "$opt") == $(basename ${desc[0]}) ] );then
         for j in $(seq 0 ${desc[1]});do
            cmd[${#cmd[@]}]="$1"
            shift
         done
         continue 2
      fi
   done
   break
done
unset args opt i j
exec "${cmd[@]}" "${inj[@]}" "$@"
